const WEEK = ['日', '一', '二', '三', '四', '五', '六']
export default Behavior({
    properties: {
        arrowColor: {
            type: String,
            value: '#50cda1'
        }
    },
    dayjs: null,
    data: {
        week: WEEK,
        nowMonth: {},
        nextMonth: {},
        prevMonth: {}
    },
    methods: {
        init(djs) {
            let date = djs
            if (!this.dayjs) {
                this.dayjs = djs
                date = this.dayjs()
            }

            let nowMonth = this.getDayList(date)
            let prevMonth = this.getDayList(date.subtract(1, 'month'))
            let nextMonth = this.getDayList(date.add(1, 'month'))
            prevMonth.list.reverse()
            prevMonth.list.length = nowMonth.starWeek
            nextMonth.list.length = 42 - nowMonth.days - nowMonth.starWeek
            const prevMonthList = prevMonth.list
            const nextMonthList = nextMonth.list

            this.setData({
                nowMonth,
                prevMonthList,
                nextMonthList,
            })
        },
        toDate(date) {
            this.init(date)
        },
        getDayList(date) {
            let {$y, $M, $D, $W} = date
            $M += 1
            const starWeek = this.dayjs(`${$y}-${$M}-1`).$W
            let item = {
                date,
                year: $y,
                month: $M,
                day: $D,
                week: $W,
                YM: date.format('YYYY年M月'),
                starWeek,
                days: date.daysInMonth(),
                key: date.format('YYYY-MM'),
                list: []
            }
            for (let i = 1; i <= item.days; i++) {
                const date = this.dayjs(item.key + i)
                let day = {
                    value: i,
                    date,
                    str: date.format('YYYY-MM-DD')
                }
                item.list.push(day)
            }
            return item
        },
        handlerNext() {
            let lastM = this.data.nowMonth.date
            this.init(lastM.add(1, 'month'))
        },
        handlerPrev() {
            let lastM = this.data.nowMonth.date
            this.init(lastM.subtract(1, 'month'))
        },
        handlerItem(e) {
            const day = e.currentTarget.dataset.item
            this.triggerEvent('checked', day)
        },
        handlerPrevItem(e) {
            const day = e.currentTarget.dataset.item
            this.triggerEvent('prevchecked', day)
        },
        handlerNextItem(e) {
            const day = e.currentTarget.dataset.item
            this.triggerEvent('nextchecked', day)
        }
    },
})
